﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using DevExpress.Web.ASPxTreeList;


public partial class WebUserControl_ControlPhanQuyenCayPhanCap : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    static DataTable db;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    static string error = "";
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDrop();
            db = null;
        }
        else
        {
            try
            {

                ASPxTreeList1.DataSource = db;
                ASPxTreeList1.DataBind();

            }
            catch
            {

            }
        }
    }
    void BindData()
    {
        try
        {
            if (DropUser.SelectedValue.ToString() != "--Chọn User--")
            {
                dt = new ClassExecute.ClassExecute();
                db = dt.Get_Data_DataTable(str, ref error, "ls_getSelSalesPKeyFilterByADNCode", CommandType.StoredProcedure,
                    new SqlParameter("@loginName", Session["username"].ToString()),
                    new SqlParameter("@userName", DropUser.SelectedValue.ToString()));
                if (db.Rows.Count > 0)
                {
                    ASPxTreeList1.DataSource = db;
                    ASPxTreeList1.DataBind();
                }
                else
                {
                    db = null;
                    ASPxTreeList1.DataSource = null;
                    ASPxTreeList1.DataBind();
                }


            }
            else
            {
                db = null;
                ASPxTreeList1.DataSource = null;
                ASPxTreeList1.DataBind();
            }
        }
        catch(Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message + error, method);
        }
    }
    void BindDrop()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable dbUser = dt.Get_Data_DataTable(str, ref error, "k_getListUserSystem", CommandType.StoredProcedure);
        DropUser.DataSource = dbUser;
        DropUser.DataTextField = "UserName";
        DropUser.DataValueField = "UserName";
        DropUser.DataBind();
    }
    protected void btXem_Click(object sender, EventArgs e)
    {
       
        BindData();
       
    }
    protected void btExpand_Click(object sender, EventArgs e)
    {
       
        ASPxTreeList1.ExpandAll();
    }
    protected void btFindNode_Click(object sender, EventArgs e)
    {
        try
        {
            
            TreeListNode node = ASPxTreeList1.FindNodeByFieldValue("salesp_key",txtNode.Text.Trim());
            node.Expanded = true;
            node.Selected = true;
            bool ch = true;
            while (ch)
            {
                try
                {

                    node = node.ParentNode;
                    node.Expanded = true;
                }
                catch
                {
                    ch = false;
                }
            }
        }
        catch
        {
            return;
        }
    }
    protected void btColap_Click(object sender, EventArgs e)
    {
       
       ASPxTreeList1.CollapseAll();
       
    }
    protected void btSave_Click(object sender, EventArgs e)
    {
        try
        {
           
            dt = new ClassExecute.ClassExecute();
            dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "ls_preInsertADNCodeByUser", ref error,
                new SqlParameter("@loginName", Session["username"].ToString()));


           foreach(TreeListNode iNode in ASPxTreeList1.Nodes)
           {
                GetSelectChildren(iNode);
           }

            dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "[ls_finishSelectADNCode4Filter]", ref error,
                new SqlParameter("@loginName", Session["username"].ToString()),
                new SqlParameter("@userName", DropUser.SelectedValue.ToString()));
            

            BindData();
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message + error, method);
        }
    }
    protected void ASPxTreeList1_HtmlRowPrepared(object sender, DevExpress.Web.ASPxTreeList.TreeListHtmlRowEventArgs e)
    {
        try
        {

            if (e.RowKind == TreeListRowKind.Data)
            {

                if (e.GetValue("msel").ToString() == "True")
                {
                    e.Row.BackColor = Color.Yellow;
                }

            }
        }
        catch
        {
        }
    }
    void GetSelectChildren(TreeListNode node)
    {

        dt = new ClassExecute.ClassExecute();

        CheckBox ch = (CheckBox)ASPxTreeList1.FindDataCellTemplateControl(node.Key, (TreeListDataColumn)ASPxTreeList1.Columns["msel"], "CheckBox1");
        if (ch.Checked)
        {
            dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "ls_insertADNCodeByUser", ref error,
           new SqlParameter("@loginName", Session["username"].ToString()),
           new SqlParameter("@userName", DropUser.SelectedValue.ToString()),
           new SqlParameter("@salesPKey", node.Key));

        }
        else
        {
            if (node.HasChildren && node.Expanded)
            {
                foreach (TreeListNode iNode1 in node.ChildNodes)
                {
                    GetSelectChildren(iNode1);
                }
            }
        }
       
    }
}
